<template>
  <el-main>
    <!-- 管理员统计 -->
    <el-row
      :gutter="20"
      type="flex"
      class="row-bg"
      justify="center"
      style="margin-bottom: 80px;"
    >
      <el-col :span="6">
        <div class="show-header" style="background: rgb(45, 183, 245);">
          <div class="show-num">{{ stuCount }}</div>
          <div class="bottom-text">学生总数</div>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="show-header" style="background: rgb(237, 64, 20);">
          <div class="show-num">{{ classCount }}</div>
          <div class="bottom-text">班级总数</div>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="show-header" style="background: rgb(45, 183, 245);">
          <div class="show-num">{{ majorCount }}</div>
          <div class="bottom-text">专业总数</div>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="show-header" style="background: rgb(255, 153, 0);">
          <div class="show-num">{{ teacherCount }}</div>
          <div class="bottom-text">教师总数</div>
        </div>
      </el-col>
    </el-row>
    <!-- 图标显示 -->
    <div style="display: flex;">
      <div id="main1" style="width: 400px;height: 300px;flex-grow: 1" />
      <div id="main2" style="width: 400px;height: 300px;flex-grow: 1" />
    </div>
    <el-card class="box-card" style="margin-top: 30px;">
      <div slot="header" class="clearfix">
        <span style="color: #000000; font-weight: 600;">公告列表</span>
      </div>
      <div v-for="(item, index) in noticeList" :key="index" class="text item">
        <span style="font-size: 14px; font-weight: 600;">
          {{ item.title }}
        </span>
        <span style="margin-left:30px; font-size: 14px;">
          {{ item.noticeContent }}
        </span>
        <span style="margin-left:30px; font-size: 14px;">
          {{ item.createTime }}
        </span>
        <el-divider />
      </div>
    </el-card>
  </el-main>

</template>

<script>
import { mapGetters } from 'vuex'
import { getHomeCountApi } from '@/api/user'
import { getTopListApi } from '@/api/notice'
import { getHotMajorApi, getStuCountApi } from '@/api/stu'
export default {
  name: 'Dashboard',
  computed: {
    ...mapGetters([
      'name',
      'roles'
    ])
  },
  data() {
    return {
      stuCount: '',
      classCount: '',
      majorCount: '',
      teacherCount: '',
      noticeList: []
    }
  },
  async mounted() {
    // 初始化
    var myChart1 = this.$echarts.init(document.getElementById('main1'))
    // 图表配置
    var option = {
      title: {
        text: '学生统计'
      },
      tooltip: {},
      xAxis: {
        data: []
      },
      yAxis: {},
      series: [
        {
          name: '总数',
          type: 'bar',
          data: []
        }
      ]
    }
    const res = await getStuCountApi()
    if (res && res.code === 200) {
      option.xAxis.data = res.data.names
      option.series[0].data = res.data.count
    }
    myChart1.setOption(option)
    var myChart2 = this.$echarts.init(document.getElementById('main2'))
    var option2 = {
      title: {
        text: '热门专业',
        left: 'center'
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        left: 'left'
      },
      series: [
        {
          name: 'Access From',
          type: 'pie',
          radius: '50%',
          data: [],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    }
    const res2 = await getHotMajorApi()
    if (res2 && res2.code === 200) {
      option2.series[0].data = res2.data
    }
    myChart2.setOption(option2)
  },
  created() {
    this.getHomeCountfo()
    this.getTopList()
  },
  methods: {
    async getTopList() {
      const res = await getTopListApi()
      if (res && res.code === 200) {
        this.noticeList = res.data
      }
    },
    async getHomeCountfo() {
      const res = await getHomeCountApi()
      if (res && res.code === 200) {
        this.stuCount = res.data.stuCount
        this.classCount = res.data.classCount
        this.majorCount = res.data.majorCount
        this.teacherCount = res.data.teacherCount
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.bottom-text {
  bottom: 0;
  width: 100%;
  background: rgba(0, 0, 0, 0.1);
  height: 25px;
  line-height: 25px;
  text-align: center;
  position: absolute;
  font-weight: 600;
}
.show-header {
  background: #00c0ef;
  color: #fff;
  height: 80px;
  border-radius: 5px;
  position: relative;
}
.show-num {
  font-size: 38px;
  font-weight: 600;
  padding: 5px;
}

</style>
